package com.hrzyit.whistle.cases.ui.fragment

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ui.fragment.BaseMvpFragment
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.data.bean.CaseIndexDeptInfoVo
import com.hrzyit.whistle.cases.injection.component.DaggerCaseCenterComponent
import com.hrzyit.whistle.cases.injection.module.CaseCenterModule
import com.hrzyit.whistle.cases.presenter.TransferSelectDeptPresenter
import com.hrzyit.whistle.cases.presenter.view.TransferSelectDeptView
import com.hrzyit.whistle.cases.ui.adapter.IndexDeptOnlySelectAdapter
import kotlinx.android.synthetic.main.fragment_select_dept_only.*
import me.yokeyword.indexablerv.IndexableLayout

/**
 * 案件管理-批转单位选择界面 Fragment，点击单位栏目跳转到人员选择界面
 * @author YangWei
 * @date 2019/4/30
 */
class IndexDeptTransferFragment : BaseMvpFragment<TransferSelectDeptPresenter>(), TransferSelectDeptView {


    private lateinit var activity: Activity

    private var userId: Long = 0L

    private var caseId: Long = 0L

    private lateinit var onlySelectIndexDeptOnlySelectAdapter: IndexDeptOnlySelectAdapter

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        activity = context as Activity

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_select_dept_only, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initAdapter()

        initData()

        //  mSelectDeptOnlyHeaderBar 的 back 按钮事件,返回的是案件详情 Activity，直接使用导航的返回按钮即可
    }


    /**
     * 初始化单位 IndexableLayout
     */
    private fun initAdapter() {
        // 初始化单位索引选择器
        // 适配器
        val indexLayout = mSelectDeptOnlyIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(activity))

        // setAdapter
        onlySelectIndexDeptOnlySelectAdapter = IndexDeptOnlySelectAdapter(activity)
        indexLayout.setAdapter(onlySelectIndexDeptOnlySelectAdapter)

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // 单位栏目点击事件
        onlySelectIndexDeptOnlySelectAdapter.setOnItemContentClickListener { _, _, _, entity ->
            // 跳转到通讯录联系人界面 Fragment
//            startActivity<SelectDeptAndUserActivity>(
//                "tag" to CaseConstant.TAG_FRAGMENT_USER_INDEX_FOR_TRANSFER,
//                "toDeptId" to entity.id,
//                "caseId" to caseId,
//                "toDeptName" to entity.simpleName
//            )
            val deptBundle = Bundle()
            deptBundle.putLong("toDeptId",entity.id)
            deptBundle.putLong("caseId",caseId)
            deptBundle.putString("toDeptName", entity.simpleName)
            val indexUserTransferFragment = IndexUserTransferFragment()
            indexUserTransferFragment.arguments = deptBundle

            val transaction = fragmentManager!!.beginTransaction()
            // 隐藏当前 fragment
//            transaction.hide(this)
            // 显示选择人员 fragment
            transaction
                .replace(
                    R.id.mSelectDeptAndUserContainer,
                    indexUserTransferFragment,
                    CaseConstant.TAG_FRAGMENT_USER_INDEX_FOR_TRANSFER
                )
            transaction.addToBackStack(null)
            transaction.commit()
        }
        // 单位标题栏目点击事件
//        onlySelectIndexDeptOnlySelectAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
//        }
    }

    /**
     * 加载数据
     */
    private fun initData() {
        // 获取案件id
        caseId = activity.intent.getLongExtra("caseId", 0L)
        // 当前操作人id
        userId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
        // 获取当前操作人所在单位可派遣人员列表
        mPresenter.selectCaseTransferDeptList(caseId, userId)
    }


    /**
     * Dagger2 注入
     */
    override fun injectComponent() {
        DaggerCaseCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .caseCenterModule(CaseCenterModule())
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    // 数据接口回调
    override fun onGetSelectCaseTransferDeptListSuccess(result: MutableList<CaseIndexDeptInfoVo>) {
        result.let {
            onlySelectIndexDeptOnlySelectAdapter.setDatas(result)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("IndexDeptTransferFr","onDestroy.....")
    }

    override fun onPause() {
        super.onPause()
        Log.e("IndexDeptTransferFr","onPause.....")
    }
}